package com.emath.objects;

import java.util.ArrayList;

import com.lnl.R;
import com.emath.shared.BigRational;
import com.emath.shared.RestClient;

public class Player {
	public static final int HAND_SIZE = 6;
	public static final int NUM_OF_STACKS = 5;
	public static boolean donewaiting;
	public static BigRational currentcardpower;
	public static String player;
	public static ArrayList<CurrentCard> mydeck;
	public static BigRational beforechallenge;
	public static boolean candiscard = true;
	public static boolean canplayresource = true;
	public static boolean challenger = false;
	public static boolean challenged = false;
	public static boolean finished = false;
	public static boolean correct;
	public static CurrentCard currentcard;
	// static String playerp;
	public static BigRational health = BigRational.valueOf("20");
	public static ArrayList<CurrentCard> myhand;
	public static ArrayList<CurrentCard> stack1;
	public static ArrayList<CurrentCard> stack2;
	public static ArrayList<CurrentCard> stack3;
	public static ArrayList<CurrentCard> stack4;
	public static ArrayList<CurrentCard> stack5;
	public static boolean waschallenged = false;
	public static boolean endturn = false;
	public static void drawinitialhand() {
		for (int i = 0; i < HAND_SIZE; i++) {
			myhand.add(mydeck.remove(0));
		}
	}

	static void drawcard(int num) {
		for (int i = 0; i < HAND_SIZE; i++) {
			if (num <= 0)
				break;
			if (myhand.get(i).cardID == 0) {
				myhand.set(i, mydeck.remove(0));
				num--;
			}
		}
	}

	static int findposition(CurrentCard card) {
		RestClient Connect = new RestClient();

		if (!card.type1.equals("Financial") && !card.type1.equals("Government")
				&& !card.type1.equals("Arena")
				&& !card.type1.equals("University")) {
			if (Game.numresources() < Integer.parseInt(card.cost))
				return -1;

		}

		for (int k = 0; k < 3; k++) {

			switch (k) {
			case 0:
				if (stack1.get(0).cardID == 0) {
					stack1.set(0, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack1=";
					for (int i = 0; i < stack1.size(); i++) {
						if (stack1.get(i).cardID != 0)
							setstacks = setstacks + stack1.get(i).cardID;
						if (i != stack1.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack1card1;
				}
				if (stack2.get(0).cardID == 0) {
					stack2.set(0, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack2=";
					for (int i = 0; i < stack2.size(); i++) {
						if (stack2.get(i).cardID != 0)
							setstacks = setstacks + stack2.get(i).cardID;
						if (i != stack2.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack2card1;
				}
				if (stack3.get(0).cardID == 0) {
					stack3.set(0, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack3=";
					for (int i = 0; i < stack3.size(); i++) {
						if (stack3.get(i).cardID != 0)
							setstacks = setstacks + stack3.get(i).cardID;
						if (i != stack3.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack3card1;
				}
				if (stack4.get(0).cardID == 0) {
					stack4.set(0, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack4=";
					for (int i = 0; i < stack4.size(); i++) {
						if (stack4.get(i).cardID != 0)
							setstacks = setstacks + stack4.get(i).cardID;
						if (i != stack4.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack4card1;
				}
				if (stack5.get(0).cardID == 0) {
					stack5.set(0, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack5=";
					for (int i = 0; i < stack5.size(); i++) {
						if (stack5.get(i).cardID != 0)
							setstacks = setstacks + stack5.get(i).cardID;
						if (i != stack5.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack5card1;
				}
			case 1:
				if (stack1.get(1).cardID == 0) {
					stack1.set(1, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack1=";
					for (int i = 0; i < stack1.size(); i++) {
						if (stack1.get(i).cardID != 0)
							setstacks = setstacks + stack1.get(i).cardID;
						if (i != stack1.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack1card2;
				}
				if (stack2.get(1).cardID == 0) {
					stack2.set(1, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack2=";
					for (int i = 0; i < stack2.size(); i++) {
						if (stack2.get(i).cardID != 0)
							setstacks = setstacks + stack2.get(i).cardID;
						if (i != stack2.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack2card2;
				}
				if (stack3.get(1).cardID == 0) {
					stack3.set(1, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack3=";
					for (int i = 0; i < stack3.size(); i++) {
						if (stack3.get(i).cardID != 0)
							setstacks = setstacks + stack3.get(i).cardID;
						if (i != stack3.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack3card2;
				}
				if (stack4.get(1).cardID == 0) {
					stack4.set(1, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack4=";
					for (int i = 0; i < stack4.size(); i++) {
						if (stack4.get(i).cardID != 0)
							setstacks = setstacks + stack4.get(i).cardID;
						if (i != stack4.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack4card2;
				}
				if (stack5.get(1).cardID == 0) {
					stack5.set(1, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack5=";
					for (int i = 0; i < stack5.size(); i++) {
						if (stack5.get(i).cardID != 0)
							setstacks = setstacks + stack5.get(i).cardID;
						if (i != stack5.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack5card2;
				}
			case 2:
				if (stack1.get(2).cardID == 0) {
					stack1.set(2, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack1=";
					for (int i = 0; i < stack1.size(); i++) {
						if (stack1.get(i).cardID != 0)
							setstacks = setstacks + stack1.get(i).cardID;
						if (i != stack1.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack1card3;
				}
				if (stack2.get(2).cardID == 0) {
					stack2.set(2, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack2=";
					for (int i = 0; i < stack2.size(); i++) {
						if (stack2.get(i).cardID != 0)
							setstacks = setstacks + stack2.get(i).cardID;
						if (i != stack2.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack2card3;
				}
				if (stack3.get(2).cardID == 0) {
					stack3.set(2, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack3=";
					for (int i = 0; i < stack3.size(); i++) {
						if (stack3.get(i).cardID != 0)
							setstacks = setstacks + stack3.get(i).cardID;
						if (i != stack3.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack3card3;
				}
				if (stack4.get(2).cardID == 0) {
					stack4.set(2, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack4=";
					for (int i = 0; i < stack4.size(); i++) {
						if (stack4.get(i).cardID != 0)
							setstacks = setstacks + stack4.get(i).cardID;
						if (i != stack4.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack4card3;
				}
				if (stack5.get(2).cardID == 0) {
					stack5.set(2, card);
					String setstacks = "http://emath-project.appspot.com/emath/emathservicehttp?function=setgame&gameID="
							+ Game.getGameID() + "&" + getPlayer() + "stack5=";
					for (int i = 0; i < stack5.size(); i++) {
						if (stack5.get(i).cardID != 0)
							setstacks = setstacks + stack5.get(i).cardID;
						if (i != stack5.size() - 1)
							setstacks = setstacks + ",";
					}
					Connect.connect(setstacks);
					Connect.clear();
					return R.id.mystack5card3;
				}
			}

		}

		return 0;

	}

	static void addCardToStack(CurrentCard card) {
		for (int i = 1; i < NUM_OF_STACKS; i++) {
			switch (i) {
			case 1:
				if (stack1.get(0).cardID == 0)
					break;
			case 2:
				break;
			case 3:
				break;
			case 4:
				break;
			case 5:
				break;
			}
		}
	}
	static void flipCard() {
		if (mydeck.size() > 0)
			addCardToStack(mydeck.remove(0));
	}

	static void drawcard() {
		drawcard(1);
	}

	public static String getPlayer() {
		return player;
	}

	public static void setPlayer(String player) {
		Player.player = player;
	}

}
